<?php
	session_start();

	include_once("../inc/parameters.php");
	include_once("../bbs/include/common.inc.php");
	include_once("../inc/db.class.php");
	include_once("../inc/JSON.php");
	include_once("../inc/common.class.php");

	$handle	= $_POST["handle"];
	$addid	= $_POST["addid"];
	$uid		= $discuz_uid;
	
	$_MyDB	=	new dbClass();
	$_MyDB->db_connect();

	switch($handle){
		//show my fund list
		case "1":
			$sql = "select b.fund_code, a.name from fund_information a, fund_collection b where a.fund_code = b.fund_code and b.uid='".$uid."' order by b.fund_code";
			$_MyDB->query($sql);
			
			$js     = "[";
			$i      = 0;
			$arr		= Array();
			
			while($row = $_MyDB->NextRecord($result))
			{
				$arr[$i] = Array("text" => urlencode($row["name"]), "id" => $row["fund_code"], "leaf" => true, "cls" => 'file');
			
				$i++;
			}
			
			$json	=	new Services_JSON();
			echo $json->encode($arr);
			break;
			
		//add user collection fund
		case "2":
			$sql		= "select count(*) from fund_collection where uid='".$uid."' and fund_code='".$addid."'";
			$_MyDB->query($sql);
			$row		= $_MyDB->NextRecord($result);

			if($row[0] == 0){
				$_MyDB->query("insert into fund_collection (uid, fund_code) values('".$uid."', '".$addid."')");
			}
			
			//add_user_collection_fund($addid);
			echo "1";
			break;
			
		//delete user collection fund
		case "3":
			$sql		= "delete from fund_collection where uid='".$uid."' and fund_code='".$addid."'";
			$_MyDB->query($sql);
			
			//del_user_collection_fund($addid);
			echo "1";
			break;
			
		//show all fund list
		case "4":
		
			$_MyDB2	=	new dbClass();
			$_MyDB2->db_connect();
			$sql = "select fund_code from fund_collection where uid='".$uid."'";
			$_MyDB2->query($sql);
			$i = 0;
			$instr = "";
			while($row2 = $_MyDB2->NextRecord($result2)){
				if($i != 0){
					$instr.= ",";
				}
				$instr.= "'".$row2[0]."'";
				$i++;
			}

			if($instr == ""){
				$sql = "select fund_code, name from fund_information order by fund_code";
			}else{
				$sql = "select fund_code, name from fund_information where fund_code not in (".$instr.") order by fund_code";
			}

			$_MyDB->query($sql);
			
			$js     = "[";
			$i      = 0;
			$arr		= Array();
			
			while($row = $_MyDB->NextRecord($result))
			{
				$arr[$i] = Array("text" => urlencode($row["name"]), "id" => $row["fund_code"], "leaf" => true, "cls" => 'file');
			
				$i++;
			}
			
			$json	=	new Services_JSON();
			echo $json->encode($arr);

			break;
			
		default:
			break;
		
	}
?>